package com.ymh.spring.learn.mq.rabbitmq;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 
 * 名称：Send<br/>
 * 描述：消息发送端<br/>
 * 创建时间：2018年4月9日 上午11:35:46 <br/>
 * 
 * @author aivin
 * @version 1.0.0
 */
public class Send {

	/**
	 * 队列名称
	 */
	private static final String QUEUE_NAME = "test";

	public static void main(String[] args) throws IOException, TimeoutException {
		//创建连接连接到RabbitMQ
		ConnectionFactory connectionFactory = new ConnectionFactory();
		//设置RabbitMQ所在的IP或者主机名
		connectionFactory.setHost("localhost");
		//创建一个连接
		Connection connection = connectionFactory.newConnection();
		//通过连接创建一个频道
		Channel channel = connection.createChannel();
		//指定一个队列
		channel.queueDeclareNoWait(QUEUE_NAME, false, false, false, null);
		//循环发送消息
		for (int i = 0; i < 100; i++) {
			String message = "Hello World-" + i;
			//往队列中发出一条消息
			channel.basicPublish("",QUEUE_NAME, null, message.getBytes());
			System.out.println("[x] Sent " + message);
		}
		//关闭频道
		channel.close();
		//关闭连接
		connection.close();
		
	}

}
